package com.cn.gmall.weball.controller;


import com.cn.gmall.common.result.Result;
import com.cn.gmall.list.client.ListFeignClient;
import com.cn.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;


import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import java.io.FileWriter;
import java.io.IOException;

@Controller
@SuppressWarnings("all")
public class IndexController {
    @Resource
    private ProductFeignClient productFeignClient;
    @Autowired
//            thymeleaf
    TemplateEngine templateEngine;



    @GetMapping({"/", "index.html"})
    public String getIndexCategory(HttpServletRequest request) {
        request.setAttribute("list", productFeignClient.getBaseCategoryList().getData());
        return "index/index";
    }
    @GetMapping("createIndex")
    @ResponseBody
    public Result createIndex(){
        //  获取后台存储的数据
        Result result = productFeignClient.getBaseCategoryList();
        //  设置模板显示的内容
        Context context = new Context();
        context.setVariable("list",result.getData());

        //  定义文件输入位置
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter("D:\\sph\\idea_sph\\autoProductHtml\\index.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //  调用process();方法创建模板
        templateEngine.process("index/index",context,fileWriter);
        return Result.ok();
    }


}
